package sqlDao;

import infrastructure.IDossier;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import services.MyBatisConnectionFactory;
import mappers.DossierMapper;


public class DossierDAO implements DossierMapper{

	private SqlSessionFactory sqlSessionFactory; 
	
	public DossierDAO(){
		sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
	}
	public void insert(IDossier dossier) {
		SqlSession session = sqlSessionFactory.openSession();
		
		try {
			session.insert("Dossier.insertDossier", dossier);
			session.commit();
		} finally {
			session.close();
		}
		
	}

	@SuppressWarnings("unchecked")
	public List<IDossier> getDossiers() {
		SqlSession session = sqlSessionFactory.openSession();		
		try {
			List<IDossier> list = session.selectList("Dossier.getDossiers");
			return list;
		}finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<IDossier> getDossiersByNomPrenom(String nom, String prenom) {
		SqlSession session = sqlSessionFactory.openSession();
		Map<String, String> map = new HashMap<String, String>();
        map.put("nom", nom);
        map.put("prenom", prenom);
        try {
			List<IDossier> dossiers = session.selectList("Dossier.getDossiersByNomPrenom",map);
			return dossiers;
		} finally {
			session.close();
		}
	}
	@SuppressWarnings("unchecked")
	public List<IDossier> getDossiersByPrenom(String prenom) {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			List<IDossier> dossiers = session.selectList("Dossier.getDossiersByPrenom",prenom);
			return dossiers;
		} finally {
			session.close();
		}
	}
	@SuppressWarnings("unchecked")
	public List<IDossier> getDossiersByNom(String nom) {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			List<IDossier> dossiers = session.selectList("Dossier.getDossiersByNom",nom);
			return dossiers;
		} finally {
			session.close();
		}
	}
	@SuppressWarnings("unchecked")
	public List<IDossier> getDossiers(String noPlaque) {
		SqlSession session = sqlSessionFactory.openSession();
		
		try {
			List<IDossier> dossiers = session.selectList("Dossier.getDossiersByNoPlaque",noPlaque);
			return dossiers;
		} finally {
			session.close();
		}
	}

	public IDossier getDossier(String noPermis) {
		SqlSession session = sqlSessionFactory.openSession();
		
		try {
			IDossier dossier = (IDossier) session.selectOne("Dossier.getDossierByNoPermis",noPermis);
			return dossier;
		} finally {
			session.close();
		}
	}

	public IDossier getDossier(int id) {
		SqlSession session = sqlSessionFactory.openSession();
		
		try {
			IDossier dossier = (IDossier) session.selectOne("Dossier.getDossierByID",id);
			return dossier;
		} finally {
			session.close();
		}
	}

}
